const { PrismaClient } = require('@prisma/client');
const fs = require('fs');

const prisma = new PrismaClient();
// 假设我们设定的最大长度
const MAX_DESCRIPTION_LENGTH = 191;
async function main() {
    // 读取JSON文件
    const movieData = JSON.parse(fs.readFileSync('./movieData.json', 'utf-8'));
    let count = 0;
    const user1 = await prisma.user.create({
        data: {
            auth0Id: 'auth0|6567e09d04cfba643ab1b510',
            email: 'zihaoli17@yahoo.ca',
            name: 'zihaoli17@yahoo.ca',
        },
    });

    const user2 = await prisma.user.create({
        data: {
            auth0Id: 'auth0|656ad71234408e731c3819e0',
            email: '312712100@qq.com',
            name: '312712100@qq.com',
        },
    });
    // 为每部电影创建两个评分记录，分别关联到用户ID 1和2
    const userIDs = [user1.id, user2.id]; // 用户ID列表
    // 遍历电影数据并插入到数据库
    for (const movie of movieData) {
        count++;
        // 提取id中的数字部分
        let description = movie.description;

        // 截断超长的描述
        if (description && description.length > MAX_DESCRIPTION_LENGTH) {
            description = description.substring(0, MAX_DESCRIPTION_LENGTH);
        }
        // 处理Genre
        const genreIds = [];
        // 转换year为DateTime
        const releaseDate = new Date(movie.year, 0, 1); // 将年份转换为DateTime

        // 创建Movie和Rating
        const createdMovie = await prisma.movie.create({
            data: {
                sortId: movie.id,
                title: movie.title,
                description: description,
                image: movie.image,
                imdbLink: movie.imdb_link,
                releaseDate: releaseDate,
                // genre: {
                //     connect: genreIds,
                // },
                // rating: {
                //     create: {
                //         score: +movie.rating,
                //         userId: count % 2 + 1,
                //         movieId: movieId
                //     },
                // },
            },
        });
        // 处理电影的每个 Genre
        for (const genreName of movie.genre) {
            // let genre = await prisma.genre.findUnique({
            //     where: { name: genreName },
            // });

            // if (!genre) {
            genre = await prisma.genre.create({
                data: { name: genreName },
            });
            // }

            // 关联 Movie 和 Genre
            await prisma.movie.update({
                where: { id: createdMovie.id },
                data: { genre: { connect: { id: genre.id } } },
            });

        }
        //
        for (const userId of userIDs) {
            // 检查该用户是否已对该电影评分
            // const existingRating = await prisma.rating.findFirst({
            //     where: {
            //         userId: userId,
            //         movieId: createdMovie.id,
            //     },
            // });

            // 如果不存在评分，则为该电影创建新的评分记录
            // if (!existingRating) {
            await prisma.rating.create({
                data: {
                    score: +movie.rating, // 假设每个用户给出相同的评分
                    userId: userId,
                    movieId: createdMovie.id,
                },
            });
            // }
        }
    }
}

main()
    .catch(e => {
        throw e
    })
    .finally(async () => {
        console.log('complete insert data')
        await prisma.$disconnect()
    });
